package com.ruoyi.kis.util;

import com.ruoyi.kis.domain.DispatchOrderS;
import com.ruoyi.kis.domain.OsInWareOrderS;
import com.ruoyi.kis.mapper.IkisMapper;
import com.ruoyi.kis.service.imp.KisServiceImp;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;

public class VerifyBaseDataUtil {
    /**
     * 此处不能被注入原因：要在类上声明@Component，并且不能声明为静态类
     */
//    @Autowired
//    private static IkisMapper kisMapper;
    private static final Logger LOGGER = LoggerFactory.getLogger(VerifyBaseDataUtil.class);

    /**
     * 验证 部门、是否存在
     */
    public static String getDeptID(String deptID, String deptCode,IkisMapper kisMapper){
        if (deptID == null && deptCode == null) {
            return null;
        }

        String deptId = null;
        if (deptID != null && !deptID.isEmpty()) {
            String deptId1 = kisMapper.queryDepartmentIDByID(deptID);
            deptId = deptId1;
        }

        if (deptId == null && deptCode != null && !deptCode.isEmpty()) {
            deptId = kisMapper.queryFDeptIDByCode(deptCode);
        }

        return deptId;
    }

    /**
     * 验证供应商是否存在
     * @param supplierId 供应商ID
     * @param supplierCode 供应商编码
     * @return true 存在  false 不存在
     */
    public static String getSupplierID(String supplierId, String supplierCode,IkisMapper kisMapper) {
        LOGGER.info("supplierId:" +  supplierId + ";supplierCode:" + supplierCode);
        String tempSupplierID = null;
        if (supplierId == null && supplierCode == null) {

            return null;
        }
        if (supplierId != null && !supplierId.isEmpty()) {
            int i = kisMapper.querySupplierIDIfExists(supplierId);
            if (i > 0) {
                tempSupplierID =  supplierId;
            }
        }
        if (tempSupplierID == null && supplierCode != null && !supplierCode.isEmpty()) {
            String queriedId = kisMapper.querySupplierIDByCode(supplierCode);
            if(queriedId != null){
                tempSupplierID = queriedId;
            }
        }
        return tempSupplierID;
    }

    /**
     * 验证购货单位/客户是否存在
     * @param customerID 购货单位
     * @param FNumber 购货单位编码
     * @param kisMapper
     * @return 购货单位ID
     */
    public static String getCustomerID(String customerID, String FNumber,IkisMapper kisMapper) {
        String tempCustomerID = null;
        if (customerID == null && FNumber == null) {
            return null;
        }
        if (customerID != null && !customerID.isEmpty()) {
            int i = kisMapper.queryOrganizationIDIfExists(customerID);
            if (i > 0) {
                tempCustomerID =  customerID;
            }
        }
        if (tempCustomerID == null && FNumber != null && !FNumber.isEmpty()) {
            String queriedId = kisMapper.queryOrganizationIDByCode(FNumber);
            if(queriedId != null){
                tempCustomerID = queriedId;
            }
        }
        return tempCustomerID;
    }
    /**
     * 获取退料通知单、收料通知单子表的FInterID、FEntryID
     * @param FBillNo 单据编号
     * @param FItemID 物料ID
     * @return true 存在  false 不存在
     */
    public static OsInWareOrderS getTlSlId(String FBillNo, String FItemID, IkisMapper kisMapper,String fBatchNo) {
        Map<String, Object> map = new HashMap<>();
        if (FBillNo == null || FItemID == null || FItemID.isEmpty() || FBillNo.isEmpty()) {
            return null;
        }
        OsInWareOrderS os = kisMapper.getTlSlId (FBillNo, FItemID,fBatchNo);
        return os;
    }
    public static DispatchOrderS getThShId(String FBillNo, String FItemID, IkisMapper kisMapper,String fBatchNo) {
        Map<String, Object> map = new HashMap<>();
        if (FBillNo == null || FItemID == null || FItemID.isEmpty() || FBillNo.isEmpty()) {
            return null;
        }
        DispatchOrderS dispatchOrderS = kisMapper.getThShId (FBillNo, FItemID,fBatchNo);
        return dispatchOrderS;
    }

}
